package client.view;

import client.DataBuffer;
import client.model.entity.MyCellRenderer;
import client.model.entity.OnlineUserListModel;
import client.util.ClientUtil;
import common.model.entity.Request;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;


/**
 * 主窗口
 */
public class MainFrame extends JFrame{

    /** 当前用户信息Lbl */
    private JLabel currentUserLbl;
    /** 在线用户列表 */
    public static JList onlineList;
    /** 在线用户数统计Lbl */
    public static JLabel onlineCountLbl;
    /** 打开聊天窗口btn*/
    //public JButton btn;

    public MainFrame() {

        this.init();
        setVisible(true);

    }

    public void init() {
        setTitle("OO主界面");
        setIconImage(Toolkit.getDefaultToolkit().getImage("images\\logo.png"));
        setMinimumSize(new Dimension(300,600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 302, 656);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JPanel northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        JLabel headLabel = new JLabel("OO号：123456");
        headLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        headLabel.setIcon(new ImageIcon("images\\head.png"));
        northPanel.add(headLabel);
        contentPane.add(northPanel, BorderLayout.NORTH);

        //btn = new JButton("开始聊天");

        //当前用户面板
        JPanel currentUserPane = new JPanel();
        currentUserPane.setLayout(new BorderLayout());
        //currentUserPane.add(btn);
        currentUserPane.add(new JLabel("当前用户"), BorderLayout.NORTH);

        //在线用户列表面板
        JPanel onlineListPane = new JPanel();
        onlineListPane.setLayout(new BorderLayout());
        onlineCountLbl = new JLabel("在线用户列表(1)");
        onlineListPane.add(onlineCountLbl, BorderLayout.NORTH);
        // 右边用户列表创建一个分隔窗格
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                onlineListPane, currentUserPane);
        splitPane.setDividerLocation(340);
        splitPane.setDividerSize(1);

        //userPanel.add(splitPane, BorderLayout.CENTER);

        //获取在线用户并缓存
        DataBuffer.onlineUserListModel = new OnlineUserListModel(DataBuffer.onlineUsers);
        //在线用户列表
        onlineList = new JList(DataBuffer.onlineUserListModel);
        onlineList.setCellRenderer(new MyCellRenderer());
        //设置为单选模式
        onlineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        onlineListPane.add(new JScrollPane(onlineList,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));

        //当前用户信息Label
        currentUserLbl = new JLabel();
        currentUserPane.add(currentUserLbl);

        //btn.addActionListener(e -> new ChatFrame());


        contentPane.add(splitPane, BorderLayout.CENTER);

        JPanel southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
        contentPane.add(southPanel, BorderLayout.SOUTH);

        JLabel menuLabel = new JLabel("");
        menuLabel.setSize(32, 32);
        menuLabel.setIcon(new ImageIcon("images\\menu.png"));
        southPanel.add(menuLabel);

        JLabel addfLabel = new JLabel("");
        addfLabel.setIcon(new ImageIcon("images\\addf.png"));
        southPanel.add(addfLabel);

        JLabel musicLabel = new JLabel("");
        musicLabel.setIcon(new ImageIcon("images\\music.png"));
        southPanel.add(musicLabel);

        //关闭窗口
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                Request req = new Request();
                req.setAction("exit");
                try {
                    ClientUtil.sendTextRequest(req);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }finally{
                    System.exit(0);
                }
            }
        });

        menuLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new ChatFrame();
            }
        });

        addfLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(null,"因能力所限，功能暂未完成。");
            }
        });

        musicLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    Runtime run = Runtime.getRuntime();
                    run.exec("D:\\Program Files\\CloudMusic\\cloudmusic.exe");// 执行程序
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

    }

}
